<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    @close="closeDialog()"
    :visible.sync="visible">
    <el-form :model="dataForm" v-show="baseInfo" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">
    <el-form-item label="申请编号" prop="applyId" v-if="dataForm.id" label-width="120px">
        <el-input v-model="dataForm.applyId" placeholder="申请编号" disabled></el-input>
    </el-form-item>
    <el-form-item label="申请描述" prop="applyDescription" label-width="120px">
        <el-input v-model="dataForm.applyDescription" placeholder="申请描述"></el-input>
    </el-form-item>
    <el-form-item label="资料开始时间" prop="applyBeginTime" label-width="120px">
        <el-input v-model="dataForm.applyBeginTime" placeholder="资料开始时间"></el-input>
    </el-form-item>
    <el-form-item label="资料结束时间" prop="applyEndTime" label-width="120px">
        <el-input v-model="dataForm.applyEndTime" placeholder="资料结束时间"></el-input>
    </el-form-item>
    <el-form-item label="使用开始时间" prop="useBeginTime" label-width="120px">
        <el-input v-model="dataForm.useBeginTime" placeholder="使用开始时间"></el-input>
    </el-form-item>
    <el-form-item label="使用结束时间" prop="useEndTime" label-width="120px">
        <el-input v-model="dataForm.useEndTime" placeholder="使用结束时间"></el-input>
    </el-form-item>
    <el-form-item label="空间范围" prop="applySpacialScale" label-width="120px">
        <el-input v-model="dataForm.applySpacialScale" placeholder="空间范围"></el-input>
    </el-form-item>
    </el-form>

    <el-steps :active="active" finish-status="success" v-if="!baseInfo">
      <el-step title="节目单配置" @click.native="changeStep(0)"></el-step>
      <el-step title="收集任务配置" @click.native="changeStep(1)"></el-step>
      <el-step title="数据分发配置" @click.native="changeStep(2)"></el-step>
    </el-steps>

    <el-form v-if="!baseInfo && active === 0" label-width="100px">
      <el-row>
        <el-col :span="19">
          <el-form-item label="节目单配置" prop="applyPlayBillId" label-width="120px">
            <el-select style="width:100%" v-model="dataForm.applyPlayBillId" filterable placeholder="请选择节目单或者添加节目单" @change="playBillChange">
              <el-option v-for="playBill in playBillList"
              :label="playBill.dataName"
              :key="playBill.id"
              :value="playBill.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="1"></el-col>
        <el-col :span="4">
          <el-button icon="el-icon-plus" type="primary" @click="addPlayBill()">新增</el-button>
        </el-col>
      </el-row>
    </el-form>

    <el-form v-if="!baseInfo && active === 1" label-width="100px">
      <el-row>
        <el-col :span="20">
          <el-form-item>
            <el-select style="width:100%" v-model="dataForm.applyGatherId" filterable placeholder="收集任务配置" @change="gatherChange">
              <el-option v-for="gatherInfo in gatherList"
              :label="gatherInfo.taskName"
              :key="gatherInfo.id"
              :value="gatherInfo.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-button icon="el-icon-plus" type="primary" @click="addGather()">新增</el-button>
        </el-col>
      </el-row>
    </el-form>

    <el-form v-if="!baseInfo && active === 2" label-width="100px">
      <el-row>
        <el-col :span="20">
          <el-form-item>
            <el-select style="width:100%" v-model="dataForm.applyDispenseId" filterable placeholder="分发任务配置" @change="dispenseChange">
              <el-option v-for="dispenseInfo in dispenseList"
              :label="dispenseInfo.taskName"
              :key="dispenseInfo.id"
              :value="dispenseInfo.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-button icon="el-icon-plus" type="primary" @click="addDispense()">新增</el-button>
        </el-col>
      </el-row>
    </el-form>

    <el-form>
      <el-form-item>
        <play-bill v-show="!baseInfo && active === 0" ref="addOrUpdatePlayBill" @refreshPlayBillData="refreshPlayBillData" @disPlayBillvisible="disPlayBillvisible"></play-bill>
        <gather-component v-show="active === 1 && showType == 'baseGatherInfo'" ref="addOrUpdategather" @openAddConfigGather="openAddConfigGather" @openAddDecodeInfo="openAddDecodeInfo" @refreshGather="refreshGather"></gather-component>
        <configgather-component v-show="active === 1 && showType == 'configGatherInfo'" ref="addOrUpdateConfigGather" @refreshAddFtpStatus="refreshAddFtpStatus" @addFtpSussess="addFtpSussess" @refreshConfigGatherDataList="refreshConfigGatherDataList"></configgather-component>
        <decode-component v-show="active === 1 && showType == 'decodeInfo'" ref="addOrUpdateDecode" @refreshDecodeData="refreshDecodeData" @disDecodeVisible="disDecodeVisible"></decode-component>
        <dispense-component  v-show="active === 2" ref="addOrUpdateDispense" @refreshDispenseDataList="refreshDispenseDataList" @disVisible="disDispenseVisible"></dispense-component>
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="danger" @click="verifyFailed()" v-if="dataForm.applyStatus === 0 && active === 0 && baseInfo">驳回</el-button>
      <el-button type="primary" @click="lastStep()" v-if="!baseInfo && active != 0">上一步</el-button>
      <el-button type="primary" @click="nextStep()" v-if="!baseInfo && canSubmit && dataForm.applyPlayBillId">下一步</el-button>
      <el-button type="primary" @click="verifyPass()" v-if="canSubmit && baseInfo">通过</el-button>
      <el-button type="primary" @click="submitPlayBill()" v-if="!baseInfo && active === 0">提交节目单</el-button>
      <el-button type="primary" @click="verifyPassAndOpenJobConfig()" v-if="canSubmit && baseInfo">通过&收集任务配置</el-button>

      <el-button type="primary" @click="gatherDataFormSubmit()" v-if="active === 1 && showType === 'baseGatherInfo' && canSubmit">保存收集任务配置</el-button>


      <el-button type="primary" @click="backToGather()" v-if="active === 1 &&  showType === 'configGatherInfo' && !addFtpStatus">返回</el-button>
      <el-button type="primary" @click="backToConfigGather()" v-if="active === 1 &&  showType === 'configGatherInfo' && addFtpStatus">返回</el-button>
      <el-button type="primary" @click="checkFtpStatus()" v-if="active === 1 &&  showType === 'configGatherInfo' && canSubmit && addFtpStatus">FTP状态检查</el-button>
      <el-button type="primary" @click="ftpDataFormSubmit()" v-if="active === 1 &&  showType === 'configGatherInfo' && canSubmit && addFtpStatus">添加FTP</el-button>
      <el-button type="primary" @click="configGatherSubmit()" v-if="active === 1 &&  showType === 'configGatherInfo' && canSubmit && !addFtpStatus">保存收集基本配置</el-button>


      <el-button type="primary" @click="backToGather()" v-if="active === 1 &&  showType == 'decodeInfo' && !addFtpStatus">返回</el-button>
      <el-button type="primary" @click="decodeFormSubmit()" v-if="active === 1 &&  showType == 'decodeInfo' && canSubmit">保存解码配置</el-button>

      <el-button type="primary" @click="dispenseDataFormSubmit()" v-if="active === 2 && canSubmit">保存分发配置</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="active > 0 && canSubmit">完成</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {getApplyInfoById, addObj, putObj, verifyFailed, verifyPass } from '@/api/gather/applyinfo'
    import {addFtpInfo, checkFtpStatus} from '@/api/gather/configftp'
    import {addDecodeInfo, putDecodeInfo, getAllPlayBillInfo, getTableInfoByPlayBillId, sampleFileDecode} from '@/api/gather/configdecode'

    import {findAllDispense} from '@/api/job/dispenseConfig'
    import {list} from '@/api/job/gather'
    import {findAllPlayBill } from '@/api/job/playBill'
    import PlayBill from '../../job/playBill/playBill-component'
    import GatherComponent from '../../job/gather/gather-component'

    
    import ConfiggatherComponent from '../configgather/configgather-component'

    import DecodeComponent from '../configdecode/configdecode-component'

    import DispenseComponent from '../../job/dispenseConfig/dispense-component'

    export default {
    data () {
      return {
        baseInfo: true,
        visible: false,
        addApply: true,
        canSubmit: false,
        addFtpStatus: false,
        showType: 'baseGatherInfo',
        active: 0,
        playBillList: [],
        gatherList: [],
        dispenseList: [],
        decodeParamMap: {
          pathReg: '',
          decodeConfig: [],
        },
        dataForm: {
          applyId: '',
          metadataId: '',
          applyDescription: '',
          applyBeginTime: '',
          applyEndTime: '',
          useBeginTime: '',
          createBy: '',
          createTime: '',
          updateBy: '',
          updateTime: '',
          applyParams: '',
          applySpacialScale: '',
          applyFtpId: '',
          applyDecodeId: '',
          applyPlayBillId: '',
          applyGatherId: '',
          applyDispenseId:'',
          applyStatus: ''
        },
        applyParamsMap: {
          ftpInfo: {},
          decodeInfo: {}
        },
        dataRule: {
          applyDescription: [
            { required: true, message: '申请描述不能为空', trigger: 'blur' }
          ],
          applyBeginTime: [
            { required: true, message: '申请资料开始时间不能为空', trigger: 'blur' }
          ],
          applyEndTime: [
            { required: true, message: '申请资料结束时间不能为空', trigger: 'blur' }
          ],
        }
      }
    },
    components: {
      PlayBill,
      GatherComponent,
      ConfiggatherComponent,
      DecodeComponent,
      DispenseComponent
    },
    methods: {
      //gather method begin
      //打开收集基本配置
      openAddConfigGather () {
        this.showType = 'configGatherInfo'
        this.$refs.addOrUpdateConfigGather.init()
        this.$refs.addOrUpdateConfigGather.setFtpInfo(this.applyParamsMap.ftpInfo)
      },
      openAddDecodeInfo () {
        this.showType = 'decodeInfo'
        this.$refs.addOrUpdateDecode.init()
        this.$refs.addOrUpdateDecode.initByEntity(this.applyParamsMap.decodeInfo)
      },
      async refreshGather (id) {
        await this.getAllGatherInfo()
        this.dataForm.applyGatherId = id
        // this.visible = false
        // this.$emit('refreshDataList')
      },
      backToGather () {
        this.showType = 'baseGatherInfo'
      },
      backToConfigGather () {
        this.addFtpStatus = false
        this.$refs.addOrUpdateConfigGather.ftpVisible = false
      },
      gatherDataFormSubmit() {
        this.$refs.addOrUpdategather.dataFormSubmit()
      },

      ftpDataFormSubmit () {
        this.$refs.addOrUpdateConfigGather.ftpDataFormSubmit()
      },
      configGatherSubmit () {
        this.$refs.addOrUpdateConfigGather.dataFormSubmit()
      },
      decodeFormSubmit () {
        this.$refs.addOrUpdateDecode.dataFormSubmit()
      },
      //gather method end

      //configGather method begin
      refreshAddFtpStatus (status) {
        this.addFtpStatus = status
      },
      addFtpSussess () {
        this.backToConfigGather()
      },
      refreshConfigGatherDataList (configGatherId) {
        this.backToGather()
        this.$refs.addOrUpdategather.setConfigGather(configGatherId)
        // this.$emit('refreshDataList')
      },
      //configGather method end

      //config decode method begin
      decodeFormSubmit () {
        this.$refs.addOrUpdateDecode.dataFormSubmit()
      },
      refreshDecodeData (decodeId) {
        this.$refs.addOrUpdategather.setDecodeId(decodeId)
      },
      disDecodeVisible () {
        this.backToGather()
      },
      //config decode method end

      //dispense method begin
      dispenseDataFormSubmit () {
        this.$refs.addOrUpdateDispense.dataFormSubmit()
      },
      refreshDispenseDataList (id) {},
      disDispenseVisible () {},
      //dispense method end

      dispenseChange (id) {
        this.dispenseList.forEach(dispenseInfo => {
          if (id == dispenseInfo.id) {
            this.$refs.addOrUpdateDispense.init(id)
          }
        })
      },
      playBillChange (id) {
        this.playBillList.forEach(playBill => {
          if (id == playBill.id) {
            this.$refs.addOrUpdatePlayBill.init(id)
          }
        })
      },
      gatherChange (id) {
        this.gatherList.forEach(gatherInfo =>{
          if (id === gatherInfo.id) {
            this.$refs.addOrUpdategather.init(id)
          }
        })
      },
      addDispense () {
         this.$refs.addOrUpdateDispense.init(this.dataForm.applyDispenseId)
      },
      addGather () {
        this.$refs.addOrUpdategather.init(this.dataForm.applyGatherId)
      },
      getAllGatherInfo () {
        list().then(response => {
          if (response.data.code === 0) {
            this.gatherList = response.data.data
          }
        })
      },
      getAllDispenseInfo () {
        findAllDispense().then(response => {
          if (response.data.code === 0) {
            this.dispenseList = response.data.data
          }
        })
      },
      refreshPlayBillData (playBillInfo) {
        this.reflshAllPlayBillInfo()
        this.dataForm.applyPlayBillId = playBillInfo.id
      },
      submitPlayBill () {
        this.$refs.addOrUpdatePlayBill.dataFormSubmit()
      },
      addPlayBill () {
        this.$refs.addOrUpdatePlayBill.init(this.dataForm.applyPlayBillId)
      },
      verifyPassAndOpenJobConfig() {
        verifyPass(this.dataForm.applyId).then(response => {
          if (response.data.code === 0) {
              this.baseInfo = false
              this.active = 0
              this.applyStatus = 1
              findAllPlayBill().then(response => {
                if (response.data.code === 0) {
                  this.playBillList = response.data.data
                }
              })
              this.getAllGatherInfo()
              this.getAllDispenseInfo()
              this.addDispense()
              this.addGather()
              this.addPlayBill()
          }
        })
      },
      verifyFailed() {
        verifyFailed(this.dataForm.applyId).then(response => {
          if (response.data.code === 0) {
            this.visible = false;
            this.$emit('refreshDataList')
            this.$notify.success('操作成功')
          }
        })
      },
      verifyPass() {
        verifyPass(this.dataForm.applyId).then(response => {
          if (response.data.code === 0) {
            // this.visible = false;
            // this.$emit('refreshDataList')
            this.baseInfo = false
            this.active = 0
            this.applyStatus = 1
            this.$notify.success('操作成功')
          }
        })
      },
      checkFtpStatus () {
        this.$refs['ftpDataForm'].validate((valid) => {
          if (valid) {
            checkFtpStatus(this.applyParamsMap.ftpInfo).then(data => {
                if (data.data.code === 0) {
                  this.$notify.success(data.data.data)
                } else {
                  this.$notify.error(data.data.data)
                }
            })
          }
        })
      },
      disPlayBillvisible () {
        this.playBillVisible = false
      },
      nextStep () {
        if (this.active++ > 2) this.active = 0;
      },
      lastStep () {
        if (this.active-- < 0) this.active = 2;
      },
      changeStep (active) {

        if (active > 2 || active < 0) {
          this.active = 0
        } else {
          this.active = active
        }
      },
      init (id) {
        this.visible = true;
        this.canSubmit = true;
        this.baseInfo = true;
        this.active = 0;
        this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            if (id) {
            getApplyInfoById(id).then(response => {
                this.dataForm = response.data.data
                this.applyParamsMap = JSON.parse(this.dataForm.applyParams)
            })
          }
          this.reflshAllPlayBillInfo()
        })
      },
      reflshAllPlayBillInfo () {
        getAllPlayBillInfo().then(response => {
          this.playBillList = response.data.data
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.canSubmit = false;
            if (this.dataForm.applyId) {
                putObj(this.dataForm).then(data => {
                    this.$notify.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            } else {
                addObj(this.dataForm).then(data => {
                    this.$notify.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            }
          }
        })
      },
      //重置表单
      closeDialog() {
        if(this.baseInfo) {
          this.$refs["dataForm"].resetFields()
        }
      }
    }
  }
</script>
